extends CharacterBody2D
class_name Box

const box_white = preload("res://sprites/box/white.svg")
const box_red = preload("res://sprites/box/red.svg")
const box_green = preload("res://sprites/box/green.svg")
const box_blue = preload("res://sprites/box/blue.svg")
const box_cyan = preload("res://sprites/box/cyan.svg")
const box_magenta = preload("res://sprites/box/magenta.svg")
const box_yellow = preload("res://sprites/box/yellow.svg")

func _ready() -> void:
	match $ColorBody.color:
		Globals.Colors.WHITE:
			$Sprite2D.texture = box_white
		Globals.Colors.RED:
			$Sprite2D.texture = box_red
		Globals.Colors.GREEN:
			$Sprite2D.texture = box_green
		Globals.Colors.BLUE:
			$Sprite2D.texture = box_blue
		Globals.Colors.CYAN:
			$Sprite2D.texture = box_cyan
		Globals.Colors.MAGENTA:
			$Sprite2D.texture = box_magenta
		Globals.Colors.YELLOW:
			$Sprite2D.texture = box_yellow

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity.y += Globals.gravity * delta

	move_and_slide()

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventMouseButton and event.pressed:
		if get_child(0).get_rect().has_point(to_local(event.position)):
			EventBus.player_moving_to_box.emit(self)
		
